<?php

namespace core\lib;

class Redis extends \Redis
{
    private static $_instance = [];

    public static function conn($select_db = 0){
        if (!isset(self::$_instance[$select_db]) || !self::$_instance[$select_db] instanceof \Redis) {
            try {
                if (!extension_loaded('redis')) {
                    throw new \BadFunctionCallException('not support: redis');
                }

                $conn = new Redis();
                $conn->connect(env('redis.host'), env('redis.port'));
                if (env('redis.auth')) {
                    $conn->auth(env('redis.auth'));
                }
                if ($select_db) {
                    $conn->select($select_db);
                }
                self::$_instance[$select_db] = $conn;
            } catch (\Exception $e) {
                die('redis connect error: ' . $e->getMessage());
            }
        }
        return self::$_instance[$select_db];
    }
}